package com.example.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.activity.AllCategoryActivity;
import com.example.dianping.R;
import com.example.utils.MyUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class GirdViewHomeNavAdapter extends BaseAdapter {

	private Context context;

	public GirdViewHomeNavAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		return MyUtils.navsSort.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.homenav_layout_item, null);
			ViewUtils.inject(viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.iv_home_nav
				.setImageResource(MyUtils.navsSortImages[position]);
		viewHolder.tv_home_nav.setText(MyUtils.navsSort[position]);

		// 选中---全部选项的点击事件
		if (position == MyUtils.navsSort.length-1) {
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,
							AllCategoryActivity.class);
					context.startActivity(intent);
				}
			});
		}

		return convertView;
	}

	private class ViewHolder {
		@ViewInject(R.id.iv_home_nav)
		ImageView iv_home_nav;
		@ViewInject(R.id.tv_home_nav)
		TextView tv_home_nav;
	}
}
